package com.cn.zpark;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 获取servletConfig对象的方式
 */
public class ServletConfigDemo01 extends HttpServlet {

    // 定义ServletConfig对象
    private ServletConfig servletConfig;

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取servlet名字
        String name = servletConfig.getServletName();
        System.out.println(name);

        // 获取初始化参数
        String uname = servletConfig.getInitParameter("username");
        String password = servletConfig.getInitParameter("pwd");
        System.out.println(uname + ": " + password);


        // 调用 GenericServlet 提供的 getServletConfig 方法获得 ServletConfig 对象
        // System.out.println(this.getServletConfig().getServletName());
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        this.servletConfig = config;
    }
}
